package Controller
{
	import Database.DatabaseController;
	
	import Events.*;
	
	import Filesystem.LessonPlanFile;
	
	import LessonPlans.*;
	import LessonPlans.Tasks.*;
	
	import UI.LessonPlanViewer.*;
	import UI.Loader.SkinLoader;
	import UI.Windows.UIAddTaskWindow;
	import UI.Windows.UIHistoryViewWindow;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	public class LessonPlanController
	{
		private static var controller:LessonPlanController;
		private var lessonPlanViewer:UILessonPlanViewer;
		private var historyLessonPlans:Vector.<ILessonPlan>;
		
		public function LessonPlanController(lessonPlanViewer:UILessonPlanViewer,accessKey:int)
		{
			if(accessKey!=908776)
				throw new Error("LessonPlanController() : Access Denied. Use init() instead");
			
			this.lessonPlanViewer = lessonPlanViewer;
			historyLessonPlans = new Vector.<ILessonPlan>();
			addEventListeners();
		}
		
		private function addEventListeners():void
		{
			this.lessonPlanViewer.addEventListener(UIToolbarEvent.ADD_TASK, openAddTaskWindow);
			this.lessonPlanViewer.addEventListener(UIToolbarEvent.CLEAR, clearLessonPlan);
			this.lessonPlanViewer.addEventListener(UIToolbarEvent.SAVE, saveLessonPlan);
			this.lessonPlanViewer.addEventListener(UIToolbarEvent.OPEN, openLessonPlan);
			this.lessonPlanViewer.addEventListener(UIToolbarEvent.HISTORY, getHistory);
		}
		
		public static function init(lessonPlanViewer:UILessonPlanViewer):LessonPlanController
		{
			if(controller==null)
				controller = new LessonPlanController(lessonPlanViewer,908776);
			return controller;
		}
		
		public function resizeWindow(w:Number,h:Number):void
		{
			lessonPlanViewer.width=w;
			lessonPlanViewer.height=h;
		}
		
		private function getHistory(e:UIToolbarEvent):void
		{
			var db:DatabaseController = DatabaseController.init();
			db.retrieveLessonPlans(openHistoryWindow);
		}
		
		private function openHistoryWindow(lessonPlans:Vector.<ILessonPlan>):void
		{
			historyLessonPlans=lessonPlans;
			var list:Vector.<String>=new Vector.<String>();
			for each(var lp:ILessonPlan in lessonPlans)
			{
				list.push(lp.title);
			}
			var skins:SkinLoader=SkinLoader.init();
			var win:UIHistoryViewWindow = new UIHistoryViewWindow(list,skins.retrieveClassAssets("UIHistoryViewWindow"));
			win.addEventListener(UIHistoryViewEvent.OPEN, openLessonPlanFromHistory);
			win.addEventListener(UIHistoryViewEvent.DELETE, deleteLessonPlanFromHistory);
			win.open();
		}
		
		private function deleteLessonPlanFromHistory(e:UIHistoryViewEvent):void
		{
			for each(var lp:ILessonPlan in historyLessonPlans)
			{
				if(lp.title==e.selection)
				{
					var db:DatabaseController=DatabaseController.init();
					db.removeLessonPlan(lp.title);
				}
			}
			
			e.target.refresh();
		}
		
		private function openLessonPlanFromHistory(e:UIHistoryViewEvent):void
		{
			e.target.removeEventListener(UIHistoryViewEvent.OPEN, openLessonPlanFromHistory);
			e.target.close();
			for each(var lp:ILessonPlan in historyLessonPlans)
			{
				if(lp.title==e.selection)
					lessonPlanViewer.updateLessonPlan(lp);
			}
		}
		
		private function openAddTaskWindow(e:UIToolbarEvent):void
		{
			var skins:SkinLoader = SkinLoader.init();
			var win:UIAddTaskWindow=new UIAddTaskWindow(skins.retrieveClassAssets("UIAddTaskWindow"));
			win.alwaysInFront=true;
			win.addEventListener(UIAddTaskWindowEvent.SUBMIT, addTaskToLessonPlan);
			win.open();
			win.width=lessonPlanViewer.width;
			win.move(win.nativeWindow.x,win.nativeWindow.y);
		}
		
		private function addTaskToLessonPlan(e:UIAddTaskWindowEvent):void
		{
			e.target.removeEventListener(UIAddTaskWindowEvent.SUBMIT, addTaskToLessonPlan);
			e.target.close();
			lessonPlanViewer.addTaskToLessonPlan(new Task(e.taskName,e.taskDescription));
		}
		
		private function clearLessonPlan(e:UIToolbarEvent):void
		{
			lessonPlanViewer.clearLessonPlan();
		}
		
		private function saveLessonPlan(e:UIToolbarEvent):void
		{
			//insert into database for history
			var db:DatabaseController = DatabaseController.init();
			db.insertLessonPlan(lessonPlanViewer.lessonPlanData);
			
			//save file to disk
			var file:LessonPlanFile = new LessonPlanFile();
			file.addEventListener(Event.SELECT,writeLessonPlanToFile);
			file.browseForSave();
		}
		
		private function writeLessonPlanToFile(e:Event):void
		{
			e.target.removeEventListener(Event.SELECT,writeLessonPlanToFile);
			e.target.write(lessonPlanViewer.lessonPlanData);
		}
		
		private function openLessonPlan(e:UIToolbarEvent):void
		{
			var file:LessonPlanFile = new LessonPlanFile();
			file.addEventListener(Event.SELECT,openLessonPlanFromFile);
			file.browseForOpen();
		}
		
		private function openLessonPlanFromFile(e:Event):void
		{
			e.target.removeEventListener(Event.SELECT,openLessonPlanFromFile);
			var lp:ILessonPlan = e.target.read();
			lessonPlanViewer.updateLessonPlan(lp);
		}
	}
}